<template>
  <div class="login-register">
    <div class="contain">
      <div class="big-box" :class="{ active: isLogin }">
        <div class="big-contain" key="bigContainLogin" v-if="isLogin">
          <div class="btitle">在线考试系统</div>
          <div class="bform">
            <input
              type="text"
              placeholder="用户姓名"
              v-model.trim="form.username"
            />
            <span class="errTips" v-if="nameError">* 用户姓名填写错误 *</span>
            <input type="password" placeholder="密码" v-model="form.password" />
            <span class="errTips" v-if="nameError">* 密码填写错误 *</span>
          </div>
          <button class="bbutton" @click="login">登录</button>
        </div>
        <div class="big-contain" key="bigContainRegister" v-else>
          <div class="btitle">注册账号</div>
          <div class="bform">
            <input type="text" placeholder="用户名" v-model="form.username" />
            <span class="errTips" v-if="existed">* 用户名已经存在！ *</span>
            <input type="password" placeholder="密码" v-model="form.password" />
            <input
              type="password"
              placeholder="确认密码"
              v-model="form.repeat"
            />
            <el-form :model="form" ref="regRef">
              <!-- 身份选择 -->
              <el-form-item
                prop="status"
                style="width: 120px; margin-top: 20px"
              >
                <el-select
                  v-model="form.status"
                  clearable
                  placeholder="身份选择"
                  ref="status"
                >
                  <el-option
                    v-for="item in statusOptions"
                    :key="item"
                    :value="item"
                  >
                  </el-option>
                </el-select>
              </el-form-item>
              <!-- 班级选择 -->
              <el-form-item
                prop="class"
                v-if="form.status === '学生'"
                style="width: 120px; margin-top: -63px; margin-left: 160px"
              >
                <el-select
                  v-model="form.class"
                  clearable
                  placeholder="选择班级"
                >
                  <el-option
                    v-for="item in classOptions"
                    :key="item"
                    :value="item"
                  >
                  </el-option>
                </el-select>
              </el-form-item>
            </el-form>
          </div>
          <button class="bbutton" @click="register">注册</button>
        </div>
      </div>
      <div class="small-box" :class="{ active: isLogin }">
        <div class="small-contain" key="smallContainRegister" v-if="isLogin">
          <div class="stitle">你好!</div>
          <p class="scontent">
            温馨提示：如果您还没有登录账号，请点击下面的注册按钮进行注册
          </p>
          <button class="sbutton" @click="changeType">注册</button>
        </div>
        <div class="small-contain" key="smallContainLogin" v-else>
          <div class="stitle">你好!</div>
          <p class="scontent">
            温馨提示：如果您已拥有登录账号，请点击下面的登录按钮进行登录
          </p>
          <button class="sbutton" @click="login">登录</button>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import axios from "axios";
import { mapMutations } from "vuex";
export default {
  name: "login-register",
  data() {
    return {
      isLogin: true,
      nameError: false,
      passwordError: false,
      existed: false,
      form: {
        username: "",
        password: "",
        repeat: "",
        status: "",
        class: "", //班级
      },
      isShow: false,
      // 选择身份
      statusOptions: ["老师", "学生"],
      // 选择班级
      classOptions: ["一班", "二班", "三班"],
      value: "",
    };
  },
  methods: {
    ...mapMutations(["setToken"]),
    changeType() {
      this.isLogin = !this.isLogin;
      this.form.username = "";
      this.form.password = "";
      this.form.repeat = "";
    },
    // 登录
    login() {
      let _this = this;
      if (this.form.username === "" || this.form.password === "") {
        alert("用户名或密码不能为空");
      } else {
        axios({
          method: "post",
          url: "http://192.168.31.241:8080/user/login",
          data: _this.form,
        })
          .then((res) => {
            console.log(1);
            console.log(res.data);
            _this.userToken = res.data.data.token;
            // 将用户token保存到vuex中
            _this.setToken({ Authorization: res.data.data.token });
            alert("登陆成功");
            if (res.data.data.role == 1) {
              _this.$router.push("/teacher/question");
            } else if (res.data.data.role == 2) {
              _this.$router.push("/student/testlist");
            }
          })
          .catch((error) => {
            console.log(2);
            console.log(error);
            alert("账号或密码错误");
          });
      }
    },
    // 注册
    register() {
      if (this.form.status == "老师") {
        this.form.status = "1";
      } else if (this.form.status == "学生") {
        this.form.status = "2";
      }
      // 进行表单预验证
      this.$refs.regRef.validate(async (valid) => {
        if (!valid) return false;
        axios({
          method: "post",
          url: "http://192.168.31.241:8080/user/register",
          data: {
            username: this.form.username,
            password: this.form.password,
            role: this.form.status,
            class1: this.form.class,
          },
          //data: _this.loginForm
        })
          .then((res) => {
            console.log(res);
            console.log("成功");
          })
          .catch((error) => {
            console.log("失败");
          });
        this.$message.success("注册成功");
        // 4. 跳转到登录页面
        this.isLogin = true;
      });
      // if (
      //   this.form.username != "" &&
      //   this.form.repeat != "" &&
      //   this.form.userpwd != ""
      // ) {
      //   if (this.form.userpwd === this.form.repeat) {
      //     // 将用户名和密码存入到localstorage中
      //     localStorage.setItem("username", this.form.username);
      //     localStorage.setItem("userpwd", this.form.password);
      //     // 将输入框清空
      //     this.form.username = "";
      //     this.form.password = "";
      //     alert("注册成功！");
      //     // 跳转登录页面
      //     this.isLogin = true;
      //   } else {
      //     alert("两次密码不一致");
      //   }
      // } else {
      //   alert("填写不能为空！");
      // }
    },
  },
};
</script>

<style scoped>
* {
  font-family: "黑体";
}
.login-register {
  width: 100%;
  height: 100vh;
  box-sizing: border-box;
}
.contain {
  width: 60%;
  height: 60%;
  position: relative;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  background-color: #fff;
  border-radius: 20px;
  box-shadow: 0 0 3px #f0f0f0, 0 0 6px #f0f0f0;
}
.big-box {
  width: 70%;
  height: 100%;
  position: absolute;
  top: 0;
  left: 30%;
  transform: translateX(0%);
  transition: all 1s;
}
.big-contain {
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
}
.btitle {
  font-size: 2em;
  font-weight: bold;
  color: #303133;
  margin-bottom: 4px;
  margin-top: 0px;
}
.bform {
  width: 100%;
  height: 58%;
  padding: 1px 0;
  display: flex;
  flex-direction: column;
  justify-content: space-around;
  align-items: center;
}
.bform .errTips {
  display: block;
  width: 50%;
  text-align: left;
  color: red;
  font-size: 0.7em;
  margin-left: 1em;
}
.bform input {
  width: 50%;
  height: 30px;
  font-size: 17px;
  border: none;
  outline: none;
  border-radius: 10px;
  padding-left: 2em;
  background-color: #f0f0f0;
}
.el-radio {
  font-size: 15px;
}
.shenfen {
  width: 40%;
  height: 22px;
  border: none;
  outline: none;
  border-radius: 10px;
  padding-left: 2em;
  background-color: #f0f0f0;
  margin-bottom: 35px;
  margin-left: -80px;
  padding-top: 10px;
  padding-left: 2px;
}
.banji {
  position: absolute;
  width: 40%;
  height: 25px;
  margin-top: 197px;
  margin-left: -80px;
  color: black;
}
.bbutton {
  width: 20%;
  height: 40px;
  border-radius: 24px;
  border: none;
  outline: none;
  background-color: #409eff;
  color: #fff;
  font-size: 0.9em;
  cursor: pointer;
  margin-top: 25px;
}
.small-box {
  width: 30%;
  height: 100%;
  font-size: 16px;
  background: linear-gradient(135deg, #409eff, #409eff);
  position: absolute;
  top: 0;
  left: 0;
  transform: translateX(0%);
  transition: all 1s;
  border-top-left-radius: inherit;
  border-bottom-left-radius: inherit;
}
.small-contain {
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
}
.stitle {
  font-size: 1.5em;
  font-weight: bold;
  color: #fff;
}
.scontent {
  font-size: 0.9em;
  color: #fff;
  text-align: center;
  padding: 2em 4em;
  line-height: 1.7em;
}
.sbutton {
  width: 60%;
  height: 40px;
  border-radius: 24px;
  border: 1px solid #fff;
  outline: none;
  background-color: transparent;
  color: #fff;
  font-size: 0.9em;
  cursor: pointer;
}

.big-box.active {
  left: 0;
  transition: all 0.5s;
}
.small-box.active {
  left: 100%;
  border-top-left-radius: 0;
  border-bottom-left-radius: 0;
  border-top-right-radius: inherit;
  border-bottom-right-radius: inherit;
  transform: translateX(-100%);
  transition: all 1s;
}
</style>
